package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/7/11 14:36
 */
public class PicturePagerFragment extends BaseFragment {
  protected static final String EXTRA_PICS =
      PicturePagerFragment.class.getSimpleName() + "EXTRA_PICS";
  protected static final String EXTRA_POSITION =
      PicturePagerFragment.class.getSimpleName() + "EXTRA_POSITION";
  @BindView(R.id.vp_pics) ViewPager vpPics;
  @BindView(R.id.tv_current_page) TextView tvCurrentPage;
  @BindView(R.id.tv_sum_page) TextView tvSumPage;
  private ArrayList<String> pics;
  private int picPosition;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom("图片查看");
    pics = getArguments().getStringArrayList(EXTRA_PICS);
    picPosition = getArguments().getInt(EXTRA_POSITION);
  }

  @Override protected int initLayoutId() {
    return R.layout.fragment_pic_pager;
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = super.onCreateView(inflater, container, savedInstanceState);
    ButterKnife.bind(this, rootView);
    return rootView;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    tvCurrentPage.setText(picPosition + 1 + "");
    tvSumPage.setText(pics.size() + "");
    vpPics.setOffscreenPageLimit(pics.size());
    FragmentManager fm = getActivity().getSupportFragmentManager();

    vpPics.setAdapter(new FragmentStatePagerAdapter(fm) {
      @Override public Fragment getItem(int position) {
        return KnowledgeForumPictureFragment.newInstance(pics.get(position));
      }

      @Override public int getCount() {
        return pics.size();
      }
    });
    vpPics.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

      @Override public void onPageSelected(int position) {
        tvCurrentPage.setText(position + 1 + "");
      }

      @Override public void onPageScrollStateChanged(int state) {

      }
    });
    vpPics.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        getActivity().finish();
      }
    });
    vpPics.setCurrentItem(picPosition);//跳转到指定图片页面,需在绑定adapter后使用
  }

  public static PicturePagerFragment newInstance(ArrayList<String> pics, int position) {
    Bundle args = new Bundle();
    args.putStringArrayList(EXTRA_PICS, pics);
    args.putInt(EXTRA_POSITION, position);
    PicturePagerFragment fragment = new PicturePagerFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
